package com.tanhua.admin.service;


import com.tanhua.admin.exception.BusinessException;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.model.domian.Analysis;
import com.tanhua.model.domian.UserYearData;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.ThisLastYearVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DashboardService {

    @Autowired
    private AnalysisMapper analysisMapper;

    public ThisLastYearVo UserYearDateS(Map map) {

       // Long sd = (Long) map.get("sd"); //开始时间
        Long sd = Long.valueOf(map.get("sd").toString());
        //Long ed = (Long) map.get("ed");
        Long ed = Long.valueOf(map.get("ed").toString());
        //Integer type = (Integer) map.get("type");
        Integer type = Integer.valueOf(map.get("type").toString());
        Date datesd = new Date(sd);
        Date dateed = new Date(ed);
        //今年的日期
        String formatsd = new SimpleDateFormat("yyyy-MM-dd").format(datesd);
        String formated = new SimpleDateFormat("yyyy-MM-dd").format(dateed);
        //去年的日期
        long lastSd = sd - 31536000000L;
        long lastEd = ed - 31536000000L;
        Date lastSdDate = new Date(lastSd);
        Date lastEdDate = new Date(lastEd);
        String formatLastsd = new SimpleDateFormat("yyyy-MM-dd").format(lastSdDate);
        String formatLasted = new SimpleDateFormat("yyyy-MM-dd").format(lastEdDate);
        ThisLastYearVo thisLastYearVo = new ThisLastYearVo();
        switch (type) {
            case 101:
                //numRegistered
                //new SimpleDateFormat().format()
                //今年的数据
                List<Analysis> AnalysisList =  analysisMapper.queryRegistered(formatsd,formated);
                List<UserYearData> userYearData = UserYearData.initRegistered(AnalysisList);
                //去年的数据
                List<Analysis> analysesLast = analysisMapper.queryRegistered(formatLastsd, formatLasted);
                List<UserYearData> userYearDataLast = UserYearData.initRegistered(analysesLast);
                thisLastYearVo.setThisYear(userYearData);
                thisLastYearVo.setLastYear(userYearDataLast);
                break;
            case 102:
                //今年的数据
                List<Analysis> AnalysisList1 =  analysisMapper.queryRegistered(formatsd,formated);
                List<UserYearData> initActive = UserYearData.initActive(AnalysisList1);
                //去年的数据
                List<Analysis> analysesLast1 = analysisMapper.queryRegistered(formatLastsd, formatLasted);
                List<UserYearData> initActiveLast = UserYearData.initActive(analysesLast1);
                thisLastYearVo.setThisYear(initActive);
                thisLastYearVo.setLastYear(initActiveLast);
                break;
            case 103:
                //今年的数据
                List<Analysis> AnalysisList2 =  analysisMapper.queryRegistered(formatsd,formated);
                List<UserYearData> initRetention1d = UserYearData.initRetention1d(AnalysisList2);
                //去年的数据
                List<Analysis> analysesLast2 = analysisMapper.queryRegistered(formatLastsd, formatLasted);
                List<UserYearData> initRetention1dLast = UserYearData.initActive(analysesLast2);
                thisLastYearVo.setThisYear(initRetention1d);
                thisLastYearVo.setLastYear(initRetention1dLast);
                break;
            default :
                throw new BusinessException(ErrorResult.error().getErrMessage());
        }
     return thisLastYearVo;
    }
}
